﻿using MediatR;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.UserInfos
{
    public class DeleteRequestHandler : IRequestHandler<DeleteRequestModel, ApiRestult>
    {
        /// <summary>
        /// 用户信息仓储
        /// </summary>
        private readonly IUserBaseRepository _userbaserepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userbaserepository"></param>
        public DeleteRequestHandler(IUserBaseRepository userbaserepository)
        {
            _userbaserepository = userbaserepository;
        }
        //public DeleteRequestHandler(IUserBaseRepository userbaserepository)
        //{
        //    userbaserepository = _userbaserepository;
        //}

        public async Task<ApiRestult> Handle(DeleteRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult();

            restult.isSuc = true;
            restult.Code = Restult.Success;
            restult.Message = "删除成功";

            var userinfo = await _userbaserepository.GetAll(a=>a.UserInfoId == request.UserInfoId);
            if(userinfo == null)
            {
                restult.isSuc = false;
                restult.Code = Restult.Fail;
                restult.Message = "删除失败,用户名不存在";
                return await Task.FromResult(restult);
            }

            userinfo.IsDeleted = true;
            await _userbaserepository.UpdateAsync(userinfo);
            return await Task.FromResult(restult);
        }
    }

    public class DeleteRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 用户主键
        /// </summary>
        public int UserInfoId { get; set; }

    }
}
